package jmail.entiy;


import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Date;

public class MimeMess {

    private MimeMessage message;
    public MimeMess(Session session) {
        this.message = new MimeMessage(session);
    }

    public MimeMess(Session session,String subject, String content) throws MessagingException {
        this(session);
        setContents(subject, content);
        setFromTo();
    }
    public void setFromTo() throws MessagingException {
        message.setFrom(new InternetAddress("luizsuper@163.com"));
        message.setRecipient(MimeMessage.RecipientType.TO,new InternetAddress("luizsuper2020@163.com"));
    }

    public void setContents(String subject, String content) throws MessagingException {
        this.setContents(subject,"UTF-8",content,"text/html;charset=UTF-8");
    }
    public void setContents(String subject,String content,Date date) throws MessagingException {
        this.setContents(subject,"UTF-8",content,"text/html;charset=UTF-8",date);
    }
    public void setContents(String subject,String character,String content,String type,Date date) throws MessagingException {
        this.setSubject(subject,character);
        this.setContent(content,type);
        message.setSentDate(date);
    }
    public void setContents(String subject,String character,String content,String type) throws MessagingException {
        this.setContents(subject, character, content, type,new Date());
    }

    public void setSubject(String subject,String character) throws MessagingException {
        message.setSubject(subject,character);
    }
    public void setContent(String content,String type) throws MessagingException {
        message.setContent(content,type);
    }

    public MimeMessage getMessage() {
        try {
            if(message.getSubject() == null){
                throw new Exception("主题为空");
            }
            if(message.getContent() == null){
                throw new Exception("内容为空");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return message;
    }
}
